<?php

require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/new_smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page.php' );


if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

$current=1;
if($_SERVER['REQUEST_METHOD'] == 'GET'){
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}
	
	$consumer_id = $_GET['consumer_id'];
	$db = new Class_DB;
	$result = $db->query("SELECT * FROM p_consumer WHERE consumer_id = $consumer_id");
	$row = mysql_fetch_array($result);
	$smarty->assign('consumer', $row);
	
	$forms = $_GET;
	$consumer_id = $_GET["consumer_id"];
	$shop_name = $_GET["shop_name"];
	$type = $_GET["type"];
	$style = $_GET["style"];
	$num = $_GET["num"];
	$pro_name = $_GET["pro_name"];
	$refer_price1 = $_GET["refer_price1"];
	$refer_price2 = $_GET["refer_price2"];
	$r_date1 = $_GET["r_date1"];
	$r_date2 = $_GET["r_date2"];
	$u_date1 = $_GET["u_date1"];
	$u_date2 = $_GET["u_date2"];
	$e_date1 = $_GET["e_date1"];
	$e_date2 = $_GET["e_date2"];
	$r = $_GET["r"];
	
	$sql ="SELECT * ";
	$sqlcount = "SELECT COUNT(*) as cnt ";
	$sqlstr = " FROM p_consumer_head a,p_consumer_detail b,p_shop c WHERE a.consumer_id=$consumer_id AND a.price_no = b.price_no AND b.shop_id = c.shop_id  ";
	$query = "&consumer_id=$consumer_id";
	
	if($shop_name != null){
		$sqlstr.=" AND c.shop_name like '%$shop_name%' ";
		$query .= "&shop_name=$shop_name";
	}
	if($type != null){
		$sqlstr.=" AND c.type like '%$type%' ";
		$query .= "&type=$type";
	}
	if($style != null){
		$sqlstr.=" AND c.style like '%$style%' ";
		$query .= "&style=$style";
	}
	if($num != null){
		$sqlstr.=" AND b.quantity = $num ";
		$query .= "&num=$num";
	}
	if($pro_name != null){
		$sqlstr.=" AND a.pro_name like '%$pro_name%' ";
		$query .= "&pro_name=$pro_name";
	}
	if($refer_price1 != null && $refer_price2 != null){
		$sqlstr.=" AND c.refer_price*b.discount between $refer_price1 and $refer_price2 ";
		$query .= "&refer_price1=$refer_price1&refer_price2=$refer_price2";
	}
	if($refer_price1 != null && $refer_price2 == null){
		$sqlstr.=" AND c.refer_price*b.discount >= $refer_price1 ";
		$query .= "&refer_price1=$refer_price1";
	}
	if($refer_price1 == null && $refer_price2 != null){
		$sqlstr.=" AND c.refer_price*b.discount <= $refer_price2 ";
		$query .= "&refer_price2=$refer_price2";
	}
	
	if($e_date1 != null && $e_date2 != null){
		$sqlstr.=" AND a.e_date between '$e_date1' and '$e_date2' ";
		$query .= "&e_date1=$e_date1&e_date2=$e_date2";
	}
	if($e_date1 != null && $e_date2 == null){
		$sqlstr.=" AND a.e_date >= '$e_date1'";
		$query .= "&e_date1=$e_date1";
	}
	if($e_date1 == null && $e_date2 != null){
		$sqlstr.=" AND a.e_date <= '$e_date2' ";
		$query .= "&e_date2=$e_date2";
	}
	
	if ( $r==1){
		$forms['period_remark']='';
		$forms['r_date1']='';
		$forms['r_date2']='';
		$forms['u_date1']='';
		$forms['u_date2']='';
	}
	
	if($r!=1){
		if($r==2){
			$period_remark=$_GET["period_remark"];
			if($period_remark != null){
				$sqlstr.=" AND a.period_remark like '%$period_remark%' ";
				$query .= "&period_remark=$period_remark";
			}
			$forms['r_date1']='';
			$forms['r_date2']='';
			$forms['u_date1']='';
			$forms['u_date2']='';
			
		}
		
		if($r==3){
			$forms['period_remark']='';
			if($r_date1 != null && $r_date2 != null){
				$sqlstr.=" AND a.period_str between '$r_date1' and '$r_date2' ";
				$query .= "&r_date1=$r_date1&r_date2=$r_date2";
			}
			if($r_date1 != null && $r_date2 == null){
				$sqlstr.=" AND a.period_str >= '$r_date1'";
				$query .= "&r_date1=$r_date1";
			}
			if($r_date1 == null && $r_date2 != null){
				$sqlstr.=" AND a.period_str <= '$r_date2' ";
				$query .= "&r_date2=$r_date2";
			}
			if($u_date1 != null && $u_date2 != null){
				$sqlstr.=" AND a.period_end between '$u_date1' and '$u_date2' ";
				$query .= "&u_date1=$u_date1&u_date2=$u_date2";
			}
			if($u_date1 != null && $u_date2 == null){
				$sqlstr.=" AND a.period_end >= '$u_date1'";
				$query .= "&u_date1=$u_date1";
			}
			if($u_date1 == null && $u_date2 != null){
				$sqlstr.=" AND a.period_end <= '$u_date2' ";
				$query .= "&u_date2=$u_date2";
			}
		}
	}
	
	
	
	$sql =  $sql.$sqlstr." order by b.did desc LIMIT $st, ".PAGE_SIZE;
	$sqlcount = $sqlcount.$sqlstr;
	
	$db = new Class_DB;
	$res = $db->query($sql);
	
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
	$row['cmoney'] = number_format(intval($row['refer_price']*$row['discount']));
		$row['money'] = $row['refer_price']*$row['discount']*$row['quantity'];
		$row['profit'] = intval((($row['money']-$row['cost_price']*$row['quantity'])/$row['money'])*100)+"%";
		$row['refer_price'] = number_format(intval($row['refer_price']));
		
		if(strtotime($row['period_end'])<strtotime(date("Y/m/d"))){
			$row['dateover']=1;
		}else{
			$row['dateover']=0;
		}
		$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
		$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
		$row['e_date']=$row['e_date']==NULL?"":date('Y/m/d',strtotime($row['e_date']));
		$row['period_str']=$row['period_str']==NULL?"":date('Y/m/d',strtotime($row['period_str']));
		$row['period_end']=$row['period_end']==NULL?"":date('Y/m/d',strtotime($row['period_end']));
		$list[] = $row;
	}
	
	$cnt = 0;
	$db->query( $sqlcount );
	$row = $db->fetch();
	$cnt = $row['cnt'];
	
	$smarty->assign('page', getPage($current, $cnt,$query));
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
	$smarty->assign('cnt', $cnt);
	$smarty->assign('consumer_id', $consumer_id);
	$smarty->assign('list', $list);
}

/*
$db = new Class_DB;
//SQLクエリー
$sql1 = "SELECT * FROM p_consumer_detail ";	
				
$res = $db->query($sql1);
	
	
while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){	
		
	$pro_names[] = $row;		
}

if($_SERVER['REQUEST_METHOD'] == 'GET'){
	
	$sql = "SELECT * FROM p_shop S,p_consumer_detail P where S.shop_id = P.shop_id";
	
	$res1 = $db->query($sql);
	
	$cnt = 0;
	
	while( $row = mysql_fetch_array($res1,MYSQL_ASSOC) ){
		$a = $row['cost_price'];
		$b = $row['refer_price'];				
		$profitability = number_format($a/$b,4);	
		$row['profitability'] = $profitability;
		
		$consumerlist[] = $row;
	
		$cnt++;		
	}
}else{	
	$forms = $_POST;
	$flag = $_POST['flag'];
	$flag = 1;
	//$shop_id = $_POST["shop_id"];
	$type = $_POST["type"];
	$style = $_POST["style"];
	$num = $_POST["num"];
	$pro_name = $_POST["pro_name"];
	$refer_price1 = $_POST["refer_price1"];
	$refer_price2 = $_POST["refer_price2"];
	$r_date1 = $_POST["r_date1"];
	$r_date2 = $_POST["r_date2"];
	$u_date1 = $_POST["u_date1"];
	$u_date2 = $_POST["u_date2"];
	
	//SQLクエリー
	$sql = "SELECT * FROM p_shop S,p_consumer_detail P where S.shop_id = P.shop_id";	
	//echo $sql;
	//f($shop_id != null){
	//	$sql = $sql." and shop_id like '%".$shop_id."%'";		
	//}
	if($type != null){
		$sql = $sql." and type like '%".$type."%'";	
	}
	if($style != null){
		$sql = $sql." and style like '%".$style."%'";			
	}
	if($num != null){
		$sql = $sql." and num =".$num;	
	}
	if($pro_name != null){
		$sql = $sql." and pro_name like '%".$pro_name."%'";	
	}
	if($refer_price1 != null || $refer_price2 != null){
		$sql = $sql." and refer_price BETWEEN '".$refer_price1."' and '".$refer_price2."'";	
	}
	if($r_date1 != null || $r_date2 != null){
		$sql = $sql." and r_date BETWEEN '".$r_date1."' and '".$r_date2."'";	
	}	
	if($u_date1 != null || $u_date2 != null){
		$sql = $sql." and u_date BETWEEN '".$u_date1."' and '".$u_date2."'";	
	}	
	
	//$db = new Class_DB;		
		
	$res1 = $db->query($sql);
	
	$cnt = 0;
	
	while( $row = mysql_fetch_array($res1,MYSQL_ASSOC) ){
		$a = $row['cost_price'];
		$b = $row['refer_price'];				
		$profitability = number_format($a/$b,4);	
		$row['profitability'] = $profitability;
		
		$consumerlist[] = $row;
	
		$cnt++;		
	}
}
*/

$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );
$smarty->assign('menu_html', $menu_html);
$smarty->display('web-admin/consumer_price.html');


?>